#include<bits/stdc++.h>
#define int long long
using namespace std;
int T,n,a[1000010],cnt[1000010],color[1000010],ans;
void dfs(int x,int lb,int lr,int summ){
    if(x==n+1){
        ans=max(ans,summ);
        return;
    }
    if(cnt[a[x]]==1){
        dfs(x+1,lb,lr,summ);
        return;
    }
    dfs(x+1,x,lr,a[x]==a[lb]?summ+a[x]:summ);
    dfs(x+1,lb,x,a[x]==a[lr]?summ+a[x]:summ);
}
signed  main(){
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    cin>>T;
    while(T--){
        cin>>n;
        ans=0;
        memset(cnt,0,sizeof(cnt));
        for(int i=1;i<=n;i++){
            cin>>a[i];
            cnt[a[i]]++;
        }
        dfs(1,0,0,0);
        cout<<ans<<endl;
    }
    return 0;
}
/*
  1  2  3  4  5  6   7  8  9  10 11 12  13 14 15
|------------------------------------------------|
|  |  |  | 2|  |   |   |  |  |  |  |  |   |  |   |
|------------------------------------------------|
|  | 3|  |  |  |   |   |  |  |  | 3|  |   |  |   |
|------------------------------------------------|
|  |  |  |  | 4|   |   |  |  |  |  |  |   |  |   |
|------------------------------------------------|
|5 |  |  |  |  |   |   |  | 5| 5|  | 5|   |  |   |
|------------------------------------------------|
|  |  |  |  |  |   |   | 6|  |  |  |  |   |  |   |
|------------------------------------------------|
|  |  | 7|  |  |   |   |  |  |  |  |  |   |  |   |
|------------------------------------------------|
|  |  |  |  |  |   |   |  |  |  |  |  |   | 8|   |
|------------------------------------------------|
|  |  |  |  |  |   | 11|  |  |  |  |  |   |  |   |
|------------------------------------------------|
|  |  |  |  |  |   |   |  |  |  |  |  | 12|  |   |
|------------------------------------------------|
|  |  |  |  |  | 13|   |  |  |  |  |  |   |  | 13|
|------------------------------------------------|
*/
